package com.nian.mongodb.service.controller;

import com.nian.common.model.ServerResponse;
import com.nian.mongodb.service.model.Company;
import com.nian.mongodb.service.model.Department;
import com.nian.mongodb.service.service.ICompanyService;
import com.nian.mongodb.service.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

/**
 * 用途：
 * 开发：zhangmj
 * 日期：2020/4/12 0012 19:02
 */
@RestController
@RequestMapping("department")
public class DepartmentController {

    @Autowired
    private IDepartmentService iDepartmentService;

    @Autowired
    private ICompanyService iCompanyService;

    @PostMapping("findByCode")
    public ServerResponse findByCode(String code){
        return ServerResponse.createBySuccess(iDepartmentService.findByCode(code));
    }

    @PostMapping("findByName")
    public ServerResponse findByName(String name){
        return ServerResponse.createBySuccess(iDepartmentService.findByName(name));
    }

    @PostMapping("save")
    public ServerResponse save(@RequestBody Department model){
        return ServerResponse.createBySuccess(iDepartmentService.save(model));
    }

    @PostMapping("saveAll")
    public ServerResponse saveAll(@RequestBody List<Department> list){
        iDepartmentService.saveAll(list);
        return ServerResponse.createBySuccess();
    }

    @PostMapping("saveAll/test")
    public ServerResponse saveAllTest(){
        Company company = iCompanyService.findByCode("GS_0001");

        List<Department> list = new LinkedList<>();
//        company = new Company("id_000005", "北京萝卜公司", "GS_0001", "这是【萝卜】的备注");
//        Department one = new Department("id_d_000001", "白萝卜部门", "d_0001", "这是【白萝卜部门】的备注", company);
//        Department two = new Department("id_d_000002", "胡萝卜部门", "d_0002", "这是【胡萝卜部门】的备注", company);
//        Department three = new Department("id_d_000003", "水萝卜部门", "d_0003", "这是【水萝卜部门】的备注", company);

        Department one = new Department("id_d_000001", "白萝卜部门", "d_0001", "这是【白萝卜部门】的备注", company.getId());
        Department two = new Department("id_d_000002", "胡萝卜部门", "d_0002", "这是【胡萝卜部门】的备注", company.getId());
        Department three = new Department("id_d_000003", "水萝卜部门", "d_0003", "这是【水萝卜部门】的备注", company.getId());
        list.add(one);
        list.add(two);
        list.add(three);
        iDepartmentService.saveAll(list);
        return ServerResponse.createBySuccess();
    }
}
